package org.example.lock;

import java.util.Arrays;
import java.util.concurrent.CountDownLatch;

public class T00_CasAndUnsafe {
    private static volatile int m = 0;
    public static void main(String[] args) throws InterruptedException {
        Thread[] threads = new Thread[100];
        CountDownLatch latch = new CountDownLatch(threads.length);
        Object o = new Object();

        for (int i = 0; i < threads.length; i++) {
            threads[i] = new Thread(()->{
                synchronized(o) {
                    for(int j = 0; j < 10000; j++) {
                        m++;
                    }
                    latch.countDown(); // 计数减一
                }
            });
        }
        Arrays.stream(threads).forEach((thread) -> thread.start());
        latch.await(); // 计数为 0 时唤醒主线程
        System.out.println(m);
    }
}
